package com.bruce.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bruce.dao.PermissionDao;
import com.bruce.model.Permission;
import com.bruce.service.PermissionService;

@RestController
@RequestMapping("/manager/permission")
public class PermissionController {
	@Autowired
	private PermissionService PermissionServer;
	@Autowired
	private PermissionDao PermissionDao;
	@PostMapping("/add")
	public Map<String, String> add( @RequestParam("name") String name, @RequestParam("url") String url ){
		Permission impl = new Permission();
		impl.setName(name);
		impl.setUrl(url);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(PermissionServer.insert(impl)) );
			reMap.put("permissionid", String.valueOf(PermissionServer.getId(impl)) );
		}catch(Exception e) {
			e.printStackTrace();
			reMap.put("status", "0" );
		}
		return reMap;
	}

	@PostMapping("/list")
	public Object findAll(
			@RequestParam(name="pgeNum", required=false, defaultValue="1") int pageNum,
			@RequestParam(name="pageSize", required= false, defaultValue = "10")int pageSize) {
		return PermissionServer.selectAll(pageNum,pageSize);

	}
	@PostMapping("/listNoPage")
	public Map<String, Object> findAllNoPage() {
		Map<String, Object> reMap = new HashMap<String, Object>();
		reMap.put("reList",PermissionDao.selectAll());
		return reMap;

	}
	@PostMapping("/update")
	public Map<String, String> up(@RequestParam("id") Integer id,@RequestParam("name") String name, @RequestParam("url") String url ){
		Permission impl = new Permission();
		impl.setName(name);
		impl.setUrl(url);
		impl.setId(id);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(PermissionServer.update(impl)) );
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;
	}
}
